package pro.softzhang.algo.lc;

import pro.softzhang.algo.flag.knowledge.KString;

@KString
public class ReplaceSpace {
    public static void main(String[] args) {

    }

    static class Solution {
        public String replaceSpace(String s) {
            StringBuilder sb = new StringBuilder();
            for (char ch : s.toCharArray()) {
                sb.append(ch == ' ' ? "%20" : ch);
            }
            return sb.toString();
        }
    }

    static class Solution1 {
        public String replaceSpace(String s) {
            // 计算空格数量
            int blankCount = 0;
            for(int i = 0; i < s.length(); i++) {
                if(s.charAt(i) == ' ') {
                    blankCount++;
                }
            }

            char[] value = new char[s.length() + blankCount * 2];
            int p1 = s.length() - 1;
            int p2 = value.length - 1;
            while(p1 >= 0) {
                char c = s.charAt(p1);
                if(c == ' ') {
                    value[p2] = '0';
                    value[p2 - 1] = '2';
                    value[p2 - 2] = '%';
                    p2 -= 3;
                } else {
                    value[p2] = c;
                    p2--;
                }
                p1--;
            }
            return String.valueOf(value);
        }
    }

    static class Solution2 {
        public String replaceSpace(String s) {
            return s.replace(" ", "%20");
        }
    }
}
